package cn.rmshadows.joffice;

import java.io.File;
import java.io.IOException;

import func_controller.BaiduSpeechRecognizeController;
import func_controller.VoskController;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

/**
 * JavaFX App mvn clean javafx:jlink
 */
public class JofficeApp extends Application {
	static final String joffice_version = "0.0.1";
	// 是否是Windows
	// 文件分隔符
	public static String file_sp = System.getProperty("file.separator");
	public static boolean isWindows = file_sp.equals("\\");
	public static int status = 0;
	public static Scene scene;
	public static Stage s_stage;
	// 是否正在改变窗口大小
	public static boolean isResizing;

	@Override
	public void start(Stage stage) throws IOException {
		printSystemInfo();
		s_stage = stage;
		stage.setTitle("Java Office");
		try {
			stage.getIcons().add(new Image(JofficeApp.class.getResourceAsStream("/icon.png")));	
		}catch (Exception e) {
			e.printStackTrace();
		}
		scene = new Scene(loadFXML("primary"), 640, 480);

		stage.setScene(scene);
		// 结束其他进程的方法
		stage.setOnCloseRequest(e -> {
			// 结束vosk
			VoskController.is_vosking = false;
			// 结束百度识别
			BaiduSpeechRecognizeController.is_bding = false;
		});
		stage.show();

		System.out.println("当前Java运行版本：" + Runtime.version().toString());

		// 获取主界面的高、宽等参数，用于调整窗口大小匹配的时候使用
		double d_pos = PrimaryController.srsp_d_pos;
		double sp_h = PrimaryController.sp_height;
		//
		scene.widthProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				// 设置分割线0.25位置
				PrimaryController.sr_sp.setDividerPosition(0, d_pos);
				if (PrimaryController.current_fv != null) {
					PrimaryController.current_fv.setPrefWidth(scene.getWidth() * (1 - d_pos));
				}
				// 设置isResizing
				isResizing = true;
			}
		});
		scene.heightProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				if (PrimaryController.current_fv != null) {
					PrimaryController.current_fv.setPrefHeight(scene.getHeight() - sp_h);
				}
			}
		});

		scene.setOnMouseExited(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				// 设置isResizing
				isResizing = true;
			}
		});

		scene.setOnMouseEntered(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				Thread resume = new Thread(new DelayResizingStatusResumeThread(200));
				resume.start();
			}
		});

	}

	public static void printSystemInfo() {
		System.out.println("====Display System Info====");
		System.out.println("java.home: " + System.getProperty("java.home"));
		System.out.println("java.version: " + System.getProperty("java.version"));
		System.out.println("java.vendor: " + System.getProperty("java.vendor"));
		System.out.println("java.vendor.url: " + System.getProperty("java.vendor.url"));
		System.out.println("JVM:\n==>>: " + System.getProperty("java.vm.specification.version"));
		System.out.println("==>>: " + System.getProperty("java.vm.specification.vendor"));
		System.out.println("==>>: " + System.getProperty("java.vm.specification.name"));
		System.out.println("==>>: " + System.getProperty("java.vm.version"));
		System.out.println("==>>: " + System.getProperty("java.vm.vendor"));
		System.out.println("==>>: " + System.getProperty("java.vm.name"));
		System.out.println("JRE:\n==>>: " + System.getProperty("java.specification.version"));
		System.out.println("==>>: " + System.getProperty("java.specification.vendor"));
		System.out.println("==>>: " + System.getProperty("java.specification.name"));
		System.out.println("java.class.version: " + System.getProperty("java.class.version"));
		System.out.println("java.class.path: " + System.getProperty("java.class.path"));
		System.out.println("java.library.path: " + System.getProperty("java.library.path"));
		System.out.println("java.io.tmpdir: " + System.getProperty("java.io.tmpdir"));
		System.out.println("java.compiler: " + System.getProperty("java.compiler"));
		System.out.println("java.ext.dirs: " + System.getProperty("java.ext.dirs"));
		System.out.println("System info:");
		System.out.println("os.name: " + System.getProperty("os.name"));
		System.out.println("os.arch: " + System.getProperty("os.arch"));
		System.out.println("os.version: " + System.getProperty("os.version"));
		System.out.println("file.separator: " + System.getProperty("file.separator"));
		System.out.println("path.separator: " + System.getProperty("path.separator"));
		System.out.println("line.separator: " + System.getProperty("line.separator"));
		System.out.println("user.name: " + System.getProperty("user.name"));
		System.out.println("user.home: " + System.getProperty("user.home"));
		System.out.println("user.dir: " + System.getProperty("user.dir"));
		System.out.println("Joffice Version:" + joffice_version);
	}

	/**
	 * 初始化 新建文件夹等
	 */
	static void initJofficeConf() {
		// 创建conf配置文件夹
		File conf = new File("conf");
		if (conf.exists()) {
			if (!conf.isDirectory()) {
				conf.delete();
				conf.mkdir();
			}
		} else {
			conf.mkdir();
		}
		// 创建res资源文件夹
		File res = new File("res");
		if (res.exists()) {
			if (!res.isDirectory()) {
				res.delete();
				res.mkdir();
			}
		} else {
			res.mkdir();
		}
	}

	static void setRoot(String fxml) throws IOException {
		scene.setRoot(loadFXML(fxml));
	}

	private static Parent loadFXML(String fxml) throws IOException {
		FXMLLoader fxmlLoader = new FXMLLoader(JofficeApp.class.getResource(fxml + ".fxml"));
		return fxmlLoader.load();
	}

	public static void main(String[] args) {
		launch();
	}

	/**
	 * 延时还原Resizing的类 构造函数：ms：延时毫秒
	 */
	class DelayResizingStatusResumeThread implements Runnable {
		int s_ms;

		public DelayResizingStatusResumeThread(int ms) {
			this.s_ms = ms;
		}

		@Override
		public void run() {
			// TODO 自动生成的方法存根
			try {
				Thread.sleep(s_ms);
			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			} finally {
				// 还原isResizing
				isResizing = false;
			}
		}

	}

}